//
//  MyCommentView.m
//  Final
//
//  Created by student14 on 2021/12/18.
//  Copyright © 2021 Lenrony. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "MyCommentView.h"
#import "../../ModelController.h"
#import "../DetailViewController.h"
#import "../ReceivedCommentsViewController.h"
#import "MJRefresh.h"

@interface MyCommentView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>{
    
    float screen_width;
    float screen_height;
}

@property(strong, nonatomic) NSMutableArray *comments;//存储评论的数组
@property(strong, nonatomic) NSMutableArray *showingComments;
@property(strong, nonatomic) NSMutableArray *rawUsers;
@property(strong, nonatomic) NSMutableArray *showingRawUsers;
@property NSInteger currentCellCount;

@property(strong, nonatomic) UIViewController* svc;
@property(strong, nonatomic) ModelController *mc;

@end

@implementation MyCommentView

- (MyCommentView *) initWithViewController: (UIViewController *)svc
                             andRawUsers: (NSMutableArray *)rawUsers
                             andComments: (NSMutableArray *)comments
{
    self = [super init];
    _mc = [ModelController getInstance];
    screen_width = [UIScreen mainScreen].bounds.size.width;
    screen_height = [UIScreen mainScreen].bounds.size.height;
    [self setFrame:CGRectMake(0, 0, screen_width, screen_height)];
    
    self.svc = svc;
    self.rawUsers = rawUsers;
    self.comments = comments;
    
    [self setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"gray.png"]]];
    [self loadCollectionViews];
    return self;
}

- (void)loadCollectionViews {
    if (self.showingComments == nil) {
        self.showingComments = [[NSMutableArray alloc] init];
    }
    if (self.showingRawUsers == nil) {
        self.showingRawUsers = [[NSMutableArray alloc] init];
    }
    self.currentCellCount = MIN(self.comments.count, 6);
    // 初始时先加载几个comment
    for (int i = 0; i < _currentCellCount; i++) {
        [self.showingComments addObject:self.comments[i]];
        [self.showingRawUsers addObject:self.rawUsers[i]];
    }
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.minimumLineSpacing = 5;
    layout.itemSize = CGSizeMake(screen_width, 450);
    layout.sectionInset = UIEdgeInsetsMake(3, 0, 0, 0);
    
    _myCommentsView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, screen_width, 600) collectionViewLayout:layout];
    _myCommentsView.backgroundColor = [UIColor clearColor];
    _myCommentsView.scrollEnabled = YES;
    _myCommentsView.userInteractionEnabled = YES;
    
    [_myCommentsView setDelegate:self];
    [_myCommentsView setDataSource:self];
    [_myCommentsView registerClass:[MyCommentCell class] forCellWithReuseIdentifier:@"Comment"];
    
    _myCommentsView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.myCommentsView.mj_header endRefreshing];
            [(ReceivedCommentsViewController *)self.svc updateCollectionView];
        });
    }];
    
    _myCommentsView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.myCommentsView.mj_footer endRefreshing];
            if (self.comments.count > self.currentCellCount) {
                NSInteger tempCount = MIN(6, self.comments.count - self.currentCellCount);
                for (NSInteger i = self.currentCellCount; i < self.currentCellCount+tempCount; i++) {
                    [self.showingComments addObject:self.comments[i]];
                    [self.showingRawUsers addObject:self.rawUsers[i]];
                }
                self.currentCellCount += tempCount;
                [self willRemoveSubview:self.myCommentsView];
                [self.myCommentsView reloadData];
                [self addSubview:self.myCommentsView];
            }
        });
    }];
    
    [self addSubview:_myCommentsView];
}

- (void) updateCollectionViewWithComments:(NSMutableArray *)comments
                              andRawUsers:(NSMutableArray *)rawUsers
{
    [self.comments removeAllObjects];
    [self.rawUsers removeAllObjects];
    for (int i = 0; i < comments.count; i++) {
        [self.comments addObject:comments[i]];
        [self.rawUsers addObject:rawUsers[i]];
    }
    [self.showingComments removeAllObjects];
    [self.showingRawUsers removeAllObjects];
    self.currentCellCount = MIN(self.comments.count, 6);
    for (int i = 0; i < self.currentCellCount; i++) {
        [self.showingComments addObject:self.comments[i]];
        [self.showingRawUsers addObject:self.rawUsers[i]];
    }
    [self.myCommentsView reloadData];
}

// 代理方法
- (UICollectionViewCell *)collectionView: (UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    MyCommentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Comment" forIndexPath:indexPath];
    [cell setComment:_showingComments[indexPath.item] andRawUser:_showingRawUsers[indexPath.item] andViewController:_svc];
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.currentCellCount;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    Comment *comment = _showingComments[indexPath.item];
    
    NSString *tempStr = comment.content;
    CGSize size1 = [tempStr boundingRectWithSize:CGSizeMake(screen_width-60, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20]} context:nil].size;
    size1 = CGSizeMake(screen_width, size1.height);
    
    tempStr = comment.raw_content;
    CGSize size2 = [tempStr boundingRectWithSize:CGSizeMake(screen_width-60, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20]} context:nil].size;
    size2 = CGSizeMake(screen_width, size1.height + size2.height + 80);
    
    return size2;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
}

@end
